package com.draven.controller;

import com.draven.common.BaseController;
import com.draven.constants.FramworkConstants;
import com.draven.entity.User;
import com.draven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Draven on 2017/4/11.
 * E-mail:draven1122@163.com
 */
@Controller
@RequestMapping("/passport/")
public class PassportController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login() {
        return "/management/login";
    }

    /**
     * 登录方法
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/signIn")
    public String signIn(HttpServletRequest request, String username, String password) {
        try {
            if (isNullOrBlank(username)) {
                return "/management/loginFailed";
            }
            if (isNullOrBlank(password)) {
                return "/management/loginFailed";
            }
            User user = userService.login(username, password);
            if (user == null) {
                return "/management/loginFailed";
            } else {
                HttpSession httpSession = request.getSession();
                httpSession.setAttribute(FramworkConstants.USER_SESSION_KEY, user);
                return "redirect:/management/";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "/management/loginFailed";
        }
    }
}
